package com.hiwan.system.web.facereco;

import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.framework.dao.plugin.PageContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.hiwan.platform.util.StackTraceUtil;
import com.hiwan.system.common.base.BaseController;
import com.hiwan.system.enumeration.ReturnCodeEnum;
import com.hiwan.system.pojo.EntryInfo;
import com.hiwan.system.pojo.FileInfo;
import com.hiwan.system.pojo.HiwanFile;
import com.hiwan.system.pojo.ReturnInfo;
import com.hiwan.system.service.facereco.FaceRecoService;
import com.hiwan.system.service.file.FileInfoService;
import com.hiwan.system.service.file.FileService;
import com.hiwan.system.util.DateUtil;
import com.hiwan.system.util.UserCacheUtils;

/**
 * 
 * Datetime   ： 2015年10月29日 上午10:48:20<br>
 * Title      :  FileController.java<br>
 * Description:   <br>
 * Company    :  hiwan<br>
 * @author cbj
 *
 */
@Controller
@RequestMapping("/FaceReco")
public class FaceRecoController extends BaseController{

	@Autowired
	private FileService fileService;
	@Autowired
	private FaceRecoService faceRecoService;


	/**
	 * 上传文件
	 * 
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping("mobile/upload")
	public @ResponseBody Map<String, Object> upload(@RequestParam("file") MultipartFile[] files,
			EntryInfo entryInfo,HttpServletResponse response) {
			String videoFilePath = null;
			String thumbnailPath = null;
			String idCardPath = null;
			String facePath = null;
		
			Map<String, Object> map = new HashMap<String, Object>();
			ReturnInfo returnInfo = new ReturnInfo();
			try{
				MultipartFile video = files[0];
				MultipartFile thumbnail = files[1];
				MultipartFile idCard = files[2];
				MultipartFile face = files[3];
				String videoName = video.getOriginalFilename();
				String thumbnailName = thumbnail.getOriginalFilename();
				String idCardName = idCard.getOriginalFilename();
				String faceName = face.getOriginalFilename();
				videoFilePath = fileService.createFile("video", videoName, video);
				thumbnailPath = fileService.createFile("video", thumbnailName, thumbnail);
				idCardPath = fileService.createFile("picture", idCardName, idCard);
				facePath = fileService.createFile("picture", faceName, face);
				entryInfo.setCreateTime(DateUtil.dateTo14String(new Date()));
				String sdrId =  UUID.randomUUID().toString();
				entryInfo.setSdrId(sdrId);
				if(entryInfo.getSdrId() == null) {
					entryInfo.setOrgId("66");
				}
				if (videoFilePath != null && idCardPath != null && facePath != null && thumbnailPath != null) {
					entryInfo.setPicFace(faceName);
					entryInfo.setPicIdCard(idCardName);
					entryInfo.setVideo(videoName);
					entryInfo.setThumbnail(thumbnailName);
					
					entryInfo.setPathFace(facePath);
					entryInfo.setPathIdCard(idCardPath);
					entryInfo.setPathVideo(videoFilePath);
					entryInfo.setPathThumbnail(thumbnailPath);
					
					faceRecoService.insertEntryInfo(entryInfo);
					returnInfo.setRetcode(ReturnCodeEnum.success.getCode());
					returnInfo.setRetmsg(ReturnCodeEnum.success.getDesc());
				}
		} catch (Exception e) {
			returnInfo.setRetcode(ReturnCodeEnum.error.getCode());
			returnInfo.setRetmsg("查询失败");
			e.printStackTrace();
		}
			map.put("data", returnInfo);
			return map;
	}
	
	/**
	 * 查询列表
	 * 
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping("mobile/queryList")
	public @ResponseBody Map<String, Object> queryList(EntryInfo entryInfo,HttpServletResponse response) {			
		Map<String, Object> map = new HashMap<String, Object>();
		ReturnInfo returnInfo = new ReturnInfo();
		try{
			List<EntryInfo> entryInfos = faceRecoService.selectEntryInfoList(entryInfo);
			returnInfo.setRetcode(ReturnCodeEnum.success.getCode());
			returnInfo.setRetmsg("查询成功");
			returnInfo.setResult(entryInfos);
		} catch (Exception e) {
			returnInfo.setRetcode(ReturnCodeEnum.error.getCode());
			returnInfo.setRetmsg("查询失败");
			e.printStackTrace();
		}
			map.put("data", returnInfo);
			return map;
	}
	
	/**
	 * 查询列表
	 * 
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping("mobile/queryListPage")
	public @ResponseBody Map<String, Object> queryListPage(EntryInfo entryInfo,HttpServletResponse response) {			
		Map<String, Object> map = new HashMap<String, Object>();
		ReturnInfo returnInfo = new ReturnInfo();
		try{
			List<EntryInfo> entryInfos = faceRecoService.selectEntryInfoListPage(entryInfo);
			returnInfo.setRetcode(ReturnCodeEnum.success.getCode());
			returnInfo.setRetmsg(ReturnCodeEnum.success.getDesc());
			returnInfo.setResult(entryInfos);
			returnInfo.setPage(PageContext.getPage());
			map.put("page", PageContext.getPage());
		} catch (Exception e) {
			returnInfo.setRetcode(ReturnCodeEnum.error.getCode());
			returnInfo.setRetmsg("查询失败");
			e.printStackTrace();
		}
			map.put("data", returnInfo);
			return map;
	}
	
	/**
	 * 更新合同贷号
	 * 
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping("mobile/updateLoanContract")
	public @ResponseBody Map<String, Object> updateLoanContract(EntryInfo entryInfo,HttpServletResponse response) {			
		Map<String, Object> map = new HashMap<String, Object>();
		ReturnInfo returnInfo = new ReturnInfo();
		try{
			int result = faceRecoService.updateLoanContract(entryInfo);
			returnInfo.setRetcode(ReturnCodeEnum.success.getCode());
			returnInfo.setRetmsg(ReturnCodeEnum.success.getDesc());
			returnInfo.setResult(result);
		} catch (Exception e) {
			returnInfo.setRetcode(ReturnCodeEnum.error.getCode());
			returnInfo.setRetmsg("查询失败");
			e.printStackTrace();
		}
			map.put("data", returnInfo);
			return map;
	}
	
}
